﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using BLL;
using DLL;

namespace CuaHangBangDia
{
    public partial class Form_TaiKhoan_Them_Sua : DevComponents.DotNetBar.RibbonForm
    {
        TAI_KHOAN taikhoan;
        bool add;
        public Form_TaiKhoan_Them_Sua()
        {
            InitializeComponent();
            add = true;
        }

        public Form_TaiKhoan_Them_Sua(TAI_KHOAN tk)
        {
            InitializeComponent();
            taikhoan = new TAI_KHOAN();
            this.taikhoan = tk;
            add = false;
        }

        TaiKhoanManager manager = new TaiKhoanManager();

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            try
            {
                TAI_KHOAN tk = new TAI_KHOAN();
                tk.ID = txtID.Text;
                tk.TaiKhoan = txtTaiKhoan.Text;
                tk.MatKhau = txtMatKhau.Text;
                if (rdCo.Checked)
                    tk.Quyen = "Yes";
                tk.Quyen = "No";
                manager.ThemTK(tk);
                MessageBox.Show("Thêm thành công!");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Có lỗi xảy ra");
            }
        }

        private void Form_TaiKhoan_Them_Sua_Load(object sender, EventArgs e)
        {
            if (!add)
            {
                txtID.Text = taikhoan.ID;
                txtTaiKhoan.Text = taikhoan.TaiKhoan;
                txtMatKhau.Text = taikhoan.MatKhau;
                if (taikhoan.Quyen == "Yes")
                    rdCo.Checked = true;
                else
                    rdKhong.Checked = true;
            }
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            try
            {
                TAI_KHOAN tk = new TAI_KHOAN();
                tk.ID = txtID.Text;
                tk.TaiKhoan = txtTaiKhoan.Text;
                if (rdCo.Checked == true)
                    tk.Quyen = "Yes";
                else
                {
                    tk.Quyen = "No";
                }
                tk.MatKhau = txtMatKhau.Text;
                manager.CapNhatTK(tk);
                MessageBox.Show("Sửa thành công");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Có lỗi xảy ra");
            }
        }
    }
}